/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.shop.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.shop.user.api.dto.ShopCouponDTO;
import com.pig4cloud.pigx.shop.user.api.entity.ShopCoupon;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUserCoupon;
import com.pig4cloud.pigx.shop.user.api.vo.ShopCouponVO;
import com.pig4cloud.pigx.shop.user.api.vo.ShopEquityVO;
import com.pig4cloud.pigx.shop.user.service.ShopCouponService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商城优惠券
 *
 * @author pigx code generator
 * @date 2023-12-03 12:01:37
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/coupon")
@Tag(description = "shop_coupon", name = "商城优惠券管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ShopCouponController {

	private final ShopCouponService shopCouponService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param shopCoupon 商城优惠券
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getShopCouponPage(Page page, ShopCoupon shopCoupon) {
		LambdaQueryWrapper<ShopCoupon> lqw = new LambdaQueryWrapper<>();
		if (!StringUtils.isEmpty(shopCoupon.getCouponName())) {
			lqw.like(ShopCoupon::getCouponName, shopCoupon.getCouponName());
		}

		if (!StringUtils.isEmpty(shopCoupon.getStatus())) {
			lqw.eq(ShopCoupon::getStatus, shopCoupon.getStatus());
		}

		if (!StringUtils.isEmpty(shopCoupon.getCouponCate())) {
			lqw.eq(ShopCoupon::getCouponCate, shopCoupon.getCouponCate());
		}

		lqw.orderByDesc(ShopCoupon::getCreateTime);
		return R.ok(shopCouponService.page(page, lqw));
	}

	/**
	 * 通过id查询商城优惠券
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(shopCouponService.getById(id));
	}

	/**
	 * 新增商城优惠券
	 * @param shopCoupon 商城优惠券
	 * @return R
	 */
	@Operation(summary = "新增商城优惠券", description = "新增商城优惠券")
	@SysLog("新增商城优惠券")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('shop_coupon_add')")
	public R save(@RequestBody ShopCoupon shopCoupon) {
		return R.ok(shopCouponService.save(shopCoupon));
	}

	/**
	 * 修改商城优惠券
	 * @param shopCoupon 商城优惠券
	 * @return R
	 */
	@Operation(summary = "修改商城优惠券", description = "修改商城优惠券")
	@SysLog("修改商城优惠券")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('shop_coupon_edit')")
	public R updateById(@RequestBody ShopCoupon shopCoupon) {
		return R.ok(shopCouponService.updateById(shopCoupon));
	}

	/**
	 * 通过id删除商城优惠券
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除商城优惠券", description = "通过id删除商城优惠券")
	@SysLog("通过id删除商城优惠券")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('shop_coupon_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(shopCouponService.removeById(id));
	}

	/**
	 * 导出excel 表格
	 * @param shopCoupon 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('shop_coupon_export')")
	public List<ShopCoupon> export(ShopCoupon shopCoupon) {
		return shopCouponService.list(Wrappers.query(shopCoupon));
	}

	/**
	 * 获取当前用户可用的优惠券列表
	 * @return
	 */
	@GetMapping("/c/getAvailableCoupon")
	public R getAvailableCoupon() {
		Long shopUserId = SecurityUtils.getUser().getId();
		return R.ok(shopCouponService.getAvailableCoupon(shopUserId));
	}

	/**
	 * 获取当前用户可用的优惠券列表
	 * @return
	 */
	@GetMapping("/c/getMyCoupon")
	public R getMyCoupon() {
		Long shopUserId = SecurityUtils.getUser().getId();
		return R.ok(shopCouponService.getMyCoupon(shopUserId));
	}

	/**
	 * 未登录的场合，获取优惠券列表
	 * @return
	 */
	@Inner(false)
	@GetMapping("/c/getCouponList")
	public R getCouponList() {
		return R.ok(shopCouponService.getCouponList(null));
	}

	/**
	 * 登录的场合，获取优惠券列表
	 * @return
	 */
	@GetMapping("/c/getCouponListByUserId")
	public R getCouponListByUserId() {
		Long userId = SecurityUtils.getUser().getId();
		return R.ok(shopCouponService.getCouponList(userId));
	}

	/**
	 * 领取优惠券
	 * @return
	 */
	@PostMapping("/c/obtain")
	public R obtainCouponByUserId(@RequestBody ShopCouponVO shopCouponVO) {
		Long userId = SecurityUtils.getUser().getId();
		return shopCouponService.obtainCouponByUserId(userId, shopCouponVO.getId());
	}

	/**
	 * 判断优惠券是否可用
	 * @return
	 */
	@Inner
	// @GetMapping("/c/checkUserCouponStatue/{shopUserId}/{couponId}")
	@PostMapping("/c/checkUserCouponStatue")
	public R checkUserCouponStatue(@RequestBody ShopUserCoupon shopUserCoupon) {
		// 需要换成领券记录id
		return shopCouponService.checkUserCouponStatue(shopUserCoupon);
	}

	/**
	 * 抵扣优惠券
	 * @param shopUserCoupon
	 * @return
	 */
	@Inner
	@PostMapping("/useUserCoupon")
	public R useUserCoupon(@RequestBody ShopUserCoupon shopUserCoupon) {
		// 需要换成领券记录id
		return R.ok(shopCouponService.useUserCoupon(shopUserCoupon.getShopUserId(), shopUserCoupon.getId(),
				shopUserCoupon.getOrderId()));
	}

	/**
	 * 根据优惠券ID获取优惠券详情
	 * @param couponId
	 * @return
	 */
	@Inner
	@GetMapping("/getCouponInfoById/{couponId}")
	public R<ShopCoupon> getCouponInfoById(@PathVariable(value = "couponId") Long couponId) {
		return R.ok(shopCouponService.getById(couponId));
	}

	/**
	 * 根据用户领取的优惠券记录id获取优惠券信息
	 * @param shopUserCouponId
	 * @return
	 */
	@Inner
	@GetMapping("/getCouponInfoByShopUserCouponId/{shopUserCouponId}")
	public R<ShopCoupon> getCouponInfoByShopUserCouponId(
			@PathVariable(value = "shopUserCouponId") Long shopUserCouponId) {
		return R.ok(shopCouponService.getCouponInfoByShopUserCouponId(shopUserCouponId));
	}

	/**
	 * 发放优惠券给指定用户
	 * @return
	 */
	@PostMapping("/giveCoupon")
	public R giveCoupon(@RequestBody List<ShopCouponVO> shopCouponVOS) {
		return shopCouponService.giveCoupon(shopCouponVOS);
	}

	/**
	 * 获取当前用户所有优惠券
	 * @return
	 */
	@GetMapping("/getUserCouponList")
	public R getUserCouponList(Page page, ShopCouponDTO shopCouponDTO) {
		return R.ok(shopCouponService.getAvailableCouponPage(page, shopCouponDTO));
	}

	/**
	 * 判断权益包是否可以退款
	 * @return
	 */
	@Inner
	@PostMapping("/checkCancelEquityCoupons")
	public R checkCancelEquityCoupons(@RequestBody ShopEquityVO shopEquityVO) {
		return shopCouponService.checkCancelEquityCoupons(shopEquityVO);
	}

	/**
	 * 处理权益包退款，优惠券抵消
	 * @return
	 */
	@Inner
	@PostMapping("/cancelEquityCoupons")
	public R cancelEquityCoupons(@RequestBody ShopEquityVO shopEquityVO) {
		return shopCouponService.cancelEquityCoupons(shopEquityVO);
	}

	/**
	 * 退回优惠券
	 * @param shopUserCouponId
	 * @return
	 */
	@Inner
	@GetMapping("/returnShopUserCoupon/{shopUserCouponId}")
	public R returnShopUserCoupon(@PathVariable(value = "shopUserCouponId") Long shopUserCouponId) {
		return shopCouponService.returnShopUserCoupon(shopUserCouponId);
	}

}
